#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>  
#include <sys/socket.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>  
#include <netdb.h>  
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <sched.h>
#include <sys/syscall.h>
#include <errno.h>


int socket_descriptor_sig;
struct sockaddr_in address_sig;
struct sockaddr_in clientAddr_sig;
int len;
signed char buf[5];

int udp_get_bitrate_init(int port){
    int i;
    int iter=0;
    int rem;  
    len = sizeof(clientAddr_sig);
	struct timeval tv;
  
    bzero(&address_sig,sizeof(address_sig));  
    address_sig.sin_family=AF_INET;  
    address_sig.sin_addr.s_addr=htonl(INADDR_ANY);
    address_sig.sin_port=htons(port);  
  
    socket_descriptor_sig=socket(AF_INET,SOCK_DGRAM,0);//IPV4
    if (bind(socket_descriptor_sig, (struct sockaddr *)&address_sig, sizeof(address_sig)) < 0)
    {
        perror("bind");
        exit(1);
    }
	

    tv.tv_sec = 0;
    tv.tv_usec = 600000;
    if (setsockopt(socket_descriptor_sig, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
        printf("socket option  SO_RCVTIMEO not support\n");
        return -1;
    }



}


int udp_get_bitrate(){
	int bitrate;
	int i;
        //sendto(socket_descriptor,buf,1400,0,(struct sockaddr *)&address,sizeof(address)); 
	i = recvfrom(socket_descriptor_sig, &bitrate, 4, 0, (struct sockaddr*)&clientAddr_sig, &len); 
	printf("%d \n",bitrate);
	return bitrate;
}


